package com.huyn.baseframework.share;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.huyn.baseframework.model.WXLoginModel;
import com.huyn.baseframework.net.BFJSONRequest;
import com.huyn.baseframework.net.HttpRequestLoader;
import com.huyn.baseframework.net.OpenApi;
import com.huyn.baseframework.utils.UserUtil;
import com.huyn.baseframework.utils.Utils;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;

import org.json.JSONObject;

import java.util.HashMap;

/**
 * Created by hill on 16/12/20.
 */
public class QQUtil {

    public static final String SHARE_TO_QQ_IMAGE_URL = "imageUrl";
    public static final String SHARE_TO_QQ_IMAGE_LOCAL_URL = "imageLocalUrl";
    public static final String SHARE_TO_QQ_TITLE = "title";
    public static final String SHARE_TO_QQ_SUMMARY = "summary";
    public static final String SHARE_TO_QQ_SITE = "site";
    public static final String SHARE_TO_QQ_TARGET_URL = "targetUrl";
    public static final String SHARE_TO_QQ_APP_NAME = "appName";
    public static final String SHARE_TO_QQ_AUDIO_URL = "audio_url";
    public static final String SHARE_TO_QQ_KEY_TYPE = "req_type";
    public static final String SHARE_TO_QQ_EXT_STR = "share_qq_ext_str";
    public static final String SHARE_TO_QQ_EXT_INT = "cflag";
    public static final int SHARE_TO_QQ_FLAG_QZONE_AUTO_OPEN = 1;
    public static final int SHARE_TO_QQ_FLAG_QZONE_ITEM_HIDE = 2;
    public static final int SHARE_TO_QQ_TYPE_DEFAULT = 1;
    public static final int SHARE_TO_QQ_TYPE_AUDIO = 2;
    public static final int SHARE_TO_QQ_TYPE_IMAGE = 5;
    public static final int SHARE_TO_QQ_TYPE_APP = 6;

    private final String TAG = "Bnf/QQUtil";

    public static String APP_ID = "1105866210";
    private static QQUtil mInstance;

    private Context mAppContext;

    private Tencent mTencent;
    private BaseUiListener mBaseUiListener = new BaseUiListener();

    public static synchronized QQUtil getInstance(Context context) {
        if(mInstance == null)
            mInstance = new QQUtil(context.getApplicationContext());
        return mInstance;
    }

    private QQUtil(Context appContext) {
        mAppContext = appContext;
        mTencent = Tencent.createInstance(APP_ID, mAppContext);
    }

    public void login(Activity activity) {
        Utils.Log(TAG, "[login]");
        if(mTencent != null)
            mTencent.login(activity, "all", mBaseUiListener);
        else
            Utils.Log(TAG, "[login] mTencent is null");
    }

    public void logout(Activity activity) {
        Utils.Log(TAG, "[logout]");
        if(mTencent != null)
            mTencent.logout(activity);
        else
            Utils.Log(TAG, "[logout] mTentcent is null");
    }

    public BaseUiListener getBaseUiListener() {
        return mBaseUiListener;
    }

    public class BaseUiListener implements IUiListener {
        @Override
        public void onCancel() {
            Utils.Log(TAG, "[onCancel]");
        }

        @Override
        public void onError(UiError arg0) {
            Utils.Log(TAG, "[onError] arg0 : " + arg0);
        }

        @Override
        public void onComplete(Object response) {
            try {
                if(response == null) {
                    Utils.Log(TAG, "[onComplete] response is null");
                    return;
                }

                Utils.Log(TAG, "[BaseUiListener onComplete] response : " + response.toString());
                String access_token= ((JSONObject) response).getString("access_token");

                doQQLogin(access_token, new Response.Listener<WXLoginModel>() {
                    @Override
                    public void onResponse(WXLoginModel response) {
                        if(response != null && response.success()) {
                            Utils.Log(TAG, "[doQQLogin onResponse]");
                            UserUtil.saveInstance(mAppContext, response.data);
                            mAppContext.sendBroadcast(new Intent(UserUtil.LOGIN_SUCCESS));
                        }
                    }

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Utils.Log(TAG, "[doQQLogin onErrorResponse]");
                    }

                    @Override
                    public void onStart() {
                        Utils.Log(TAG, "[doQQLogin onStart]");
                    }
                });
            } catch (Exception e) {
                Utils.Log(TAG, "[onComplete] exception : " + e);
            }
        }
    }

    private void doQQLogin(String accessToken, Response.Listener<WXLoginModel> listener) {
        Utils.Log(TAG, "[doQQLogin]");
        HashMap<String, String> parameterMap = new HashMap<>();
        parameterMap.put("accessToken", accessToken);
        parameterMap.put(OpenApi.API_METHOD, OpenApi.QQ_LOGIN);
        BFJSONRequest request = new BFJSONRequest(WXLoginModel.class, parameterMap, listener);
        HttpRequestLoader.getInstance(mAppContext).startCacheLoader("", request, true);
    }

    public void share(Activity activity, Bundle bundle) {
        mTencent.shareToQQ(activity, bundle , mBaseUiListener);
    }

}